﻿using System;
using Factory;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.Common;
using System.Data;
using Basic.第一章;

namespace DesignPatternDemoTest.设计模式测试.工厂模式
{
    public class ClientA//实现工厂模式的客户代码
    {
        private readonly IFactory _factory;
        public ClientA(IFactory factory)//通过构造器方式注入
        {
            if (factory == null) throw new ArgumentNullException();
            _factory = factory;
        }
        public IProduct GetProduct() => _factory.Create();
    }

    public class ClientBatch//实现批量工厂的客户代码
    {
        /// <summary>
        /// 实际项目中，可以通过Assembler从外部把Director注入
        /// </summary>
        private DirectorBase _director = new ProductDirector();

        public IProduct[] Produce()
        {
            var collection = new ProductCollection();
            foreach (var decision in _director.Decisions)
            {
                collection += decision.Factory.Create(decision.Quantity);
            }
            return collection.Data;
        }
    }
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void BatchTest()
        {
            var client = new ClientBatch();
            var products = client.Produce();
            Assert.AreEqual(2 + 3, products.Length);
            for (var i = 0; i < 2; i++) { Assert.AreEqual("A", products[i].Name); }
            for (var i = 2; i < 5; i++) { Assert.AreEqual("B", products[i].Name); }
        }


        [TestMethod]
        public void Test()//单一工厂的实现
        {
            var factory = new Assembler().Create<IFactory>();
            var clienta = new ClientA(factory);//注入IFactory
            var product = clienta.GetProduct();//通过IFactory获取IProduct
            Assert.AreEqual("A", product.Name);
        }

        [TestMethod]
        public void TestMethod1()
        {
            var factory = new Simple.Factory();
            var product = factory.Create();
            Assert.AreEqual(product.GetType(), typeof(Simple.ConcreteProductA));
        }








        //public class DataBase
        //{
        //    private string _connectionString;
        //    private DbProviderFactory _dbProviderFactory;
        //    protected DataBase(string connectionString, DbProviderFactory dbProviderFactory)
        //    {
        //        _connectionString = connectionString;
        //        _dbProviderFactory = dbProviderFactory;
        //    }

        //    public virtual DbConnection CreateConnection()
        //    {
        //        DbConnection newConnection = _dbProviderFactory.CreateConnection();
        //        newConnection.ConnectionString = _connectionString;
        //        return newConnection;
        //    }
        //}



    }
}
